//
//  AppConfig.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/3/19.
//

#import "AppConfig.h"

@interface AppConfig () {
    NSString *_filePath;
    NSMutableDictionary *_configures;
}

@end

@implementation AppConfig

+ (instancetype)shared {
    static AppConfig *config = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        config = [[AppConfig alloc] init];
    });
    return config;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        NSString *documentPath = [NSString getDocumentPath];
        _filePath = [NSString stringWithFormat:@"%@/appConfig.plist",documentPath];
        NSLog(@"filePath：%@", _filePath);
        _configures = [[NSMutableDictionary alloc] initWithContentsOfFile:_filePath];
        if (_configures == nil) {
            _configures = [[NSMutableDictionary alloc] init];
        }
        [self loadConfigData];
        
    }
    return self;
}


- (void)loadConfigData {
    
    if (_configures.count > 0) {
                
        self.isLogin = [[_configures objectForKey:@"isLogin"] boolValue];
        self.isShowGuide = [[_configures objectForKey:@"isShowGuide"] boolValue];
        self.isPrivacyPolicy = [[_configures objectForKey:@"isPrivacyPolicy"] boolValue];
        self.appBuildVersion = [_configures objectForKey:@"appBuildVersion"];
        self.weiboCode = [_configures objectForKey:@"weiboCode"];
        self.weiboAccess_Token = [_configures objectForKey:@"weiboAccess_Token"];
        self.weiboUid = [_configures objectForKey:@"weiboUid"];
    }else {
        self.isLogin = NO;
        self.isShowGuide = YES;
        self.isPrivacyPolicy = YES;
        self.appBuildVersion = @"";
        self.weiboCode = @"";
        self.weiboAccess_Token = @"";
        self.weiboUid = @"";
        [self synchronize];
    }
}

- (void)synchronize {
    
    [_configures setObject:[NSNumber numberWithBool:self.isLogin] forKey:@"isLogin"];
    [_configures setObject:[NSNumber numberWithBool:self.isShowGuide] forKey:@"isShowGuide"];
    [_configures setObject:[NSNumber numberWithBool:self.isPrivacyPolicy] forKey:@"isPrivacyPolicy"];
    
    [_configures setObject:SafeStr(self.appBuildVersion) forKey:@"appBuildVersion"];
    [_configures setObject:SafeStr(self.weiboCode) forKey:@"weiboCode"];
    [_configures setObject:SafeStr(self.weiboAccess_Token) forKey:@"weiboAccess_Token"];
    [_configures setObject:SafeStr(self.weiboUid) forKey:@"weiboUid"];
    
    [_configures writeToFile:_filePath atomically:NO];
    
}

- (void)cleanAppConfig {
    
    [AppConfig shared].isLogin = NO;
    [AppConfig shared].isShowGuide = YES;
    [AppConfig shared].isPrivacyPolicy = YES;
    
    [AppConfig shared].appBuildVersion = @"";
    [AppConfig shared].weiboCode = @"";
    [AppConfig shared].weiboAccess_Token = @"";
    [AppConfig shared].weiboUid = @"";
    [[AppConfig shared] synchronize];
}

/**
 用户数据
  {"user_type":"管理员","isLogin":true,"user_password":"123456","cart_id":"SRCJZBCI2022219172249","user_name":"13811112222"}
 */

+ (BOOL)isLogin {
    NSMutableArray *loginArray = [kUserDefaultsSington valueForKey:kIsLoginArray];
    NSMutableDictionary *loginDict = [loginArray objectAtIndex:0];
    NSLog(@"USER_DATA：%@", loginDict.mj_JSONString);
    BOOL isLogin = [loginDict objectForKey:@"isLogin"];
    return isLogin;
}

+ (BOOL)isAdministrator {
    NSMutableArray *loginArray = [kUserDefaultsSington valueForKey:kIsLoginArray];
    NSMutableDictionary *loginDict = [loginArray objectAtIndex:0];
    NSString *user_type = [loginDict objectForKey:@"user_type"];
    if([user_type isEqualToString:@"管理员"]) {
        return YES;
    }
    return NO;
}


@end
